package com.srpingde.hhh;

import java.io.File;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

import de.mindpipe.android.logging.log4j.LogConfigurator;

public class LoginActivity extends Activity {
	EditText name,password;
	ImageView back;
	HHHIml weiQuIml;
	LinearLayout login_ly;
	static LoginActivity activity;
	//CheckBox login_check;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        name = (EditText) findViewById(R.id.login_name);
        name.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				 temp = s;

			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				selectionStart = name.getSelectionStart();
                selectionEnd = name.getSelectionEnd();
                if (temp.length() > 10) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    name.setText(s);
                    name.setSelection(tempSelection);//设置光标在最后
                    HhhApp.getInstance().showDialog(LoginActivity.this, "长度为10个字符！");
                }
				
				
			}
		});
        password = (EditText) findViewById(R.id.login_password);
        password.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				 temp = s;

			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				selectionStart = password.getSelectionStart();
                selectionEnd = password.getSelectionEnd();
                if (temp.length() > 10) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    password.setText(s);
                    password.setSelection(tempSelection);//设置光标在最后
                    HhhApp.getInstance().showDialog(LoginActivity.this, "长度为10个字符！");
                }
				
				
			}
		});
        back = (ImageView) findViewById(R.id.back);
        login_ly = (LinearLayout) findViewById(R.id.login_ly);
        activity = this;
       // login_check = (CheckBox) findViewById(R.id.login_check);
        layout();
      /*  login_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					WeiQuApp.getInstance().setLogin(true);
				}else{
					WeiQuApp.getInstance().setLogin(false);
				}
			}
		});*/
    }
    public static LoginActivity getInstance(){
    	return activity;
    }
    public void onClick(View v){
    	switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		case R.id.register_btn:
			startActivity(new Intent(this,RegisterActivity.class));
			break;
		case R.id.login_btn:
			if(!name.getText().toString().equals("") && !password.getText().toString().equals("")){
				weiQuIml = new HHHIml();
				weiQuIml.loginHttp(LoginActivity.this,HHHInterface.httpHeader + "api.php?act=login&account="+name.getText().toString().trim()+"&pwd="+password.getText().toString(), new AsyncHttpResponseHandler(){

						@Override
						public void onSuccess(String content) {
							// TODO Auto-generated method stub
							super.onSuccess(content);
							try {
								final JSONObject jsonObject = new JSONObject(content);
								if(jsonObject.getBoolean("return")){
									new AlertDialog.Builder(LoginActivity.this).setTitle("提示").setMessage("登录成功").setPositiveButton("确定", new DialogInterface.OnClickListener() {
										
										@Override
										public void onClick(DialogInterface dialog, int which) {
											// TODO Auto-generated method stub
											HhhApp.getInstance().setUname(name.getText().toString());
											try {
												JSONObject object = jsonObject.getJSONObject("info");
												HhhApp.getInstance().setUid(object.getString("uid"));
												finish();	
											} catch (JSONException e) {
												// TODO Auto-generated catch block
												e.printStackTrace();
											}
										}
									}).create().show();
								}else{
									HhhApp.getInstance().showDialog(LoginActivity.this, "登录失败，密码错误");
								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
				});
	    	}else{
	    		new AlertDialog.Builder(LoginActivity.this)
	    		.setTitle("提示")
	    		.setMessage("用户名和密码不能为空").setPositiveButton("确定", null).create().show();
	    	}
			break;
		}
    }
    private void layout() {
    	//输入框和文字的padding值
    	int paddingWidth_tv = (int)(HhhApp.getInstance().getDisplayHightAndWightPx()[1] * 0.105);
    	//int paddingWidth = (int)(WeiQuApp.getInstance().getDisplayHightAndWightPx()[1] * 0.15);
    	//name.setPadding(paddingWidth, 0, paddingWidth, 0);
    	//password.setPadding(paddingWidth, 0, paddingWidth, 0);
    	//((TextView)findViewById(R.id.login_name_tv)).setPadding(paddingWidth_tv, 0, 0, 0);
    	//((TextView)findViewById(R.id.login_password_tv)).setPadding(paddingWidth_tv, 0, 0, 0);
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		//
		RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
		//editLayout(name);
		//editLayout(password);
    	RelativeLayout.LayoutParams login_lyLayoutParams = (LayoutParams) login_ly.getLayoutParams();
    	login_lyLayoutParams.setMargins(paddingWidth_tv, paddingWidth_tv, paddingWidth_tv, 0);
    	login_ly.setLayoutParams(login_lyLayoutParams);
    }
    /*private void editLayout(EditText editText){
    	LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) editText.getLayoutParams();
    	layoutParams.setMargins((int)(WeiQuApp.getInstance().getDisplayHightAndWightPx()[1] * 0.105), 0, (int)(WeiQuApp.getInstance().getDisplayHightAndWightPx()[1] * 0.105), 0);
    	editText.setLayoutParams(layoutParams);
    }*/
}
